<?php
namespace app\controllers;
use yii\db\Query;
use app\forms\BookmarkForm;
use app\models\Bookmark;
use app\actions\FormAction;

class BookmarkController extends AjaxController
{
    use NeedUser;
    
    public function actions() {
    	$userId = \Yii::$app->getUser()->getId();
    	return [
    		'create' => [
    			'class' => 'app\actions\FormAction',
    			'model' => [
    				'class' => 'app\forms\BookmarkForm',
    				'scenario' => BookmarkForm::SCENARIO_CREATE,
    				'userId' => $userId,
    			]
    		],
    		'modify' => [
    			'class' => 'app\actions\FormAction',
    			'model' => [
    				'class' => 'app\forms\BookmarkForm',
    				'scenario' => BookmarkForm::SCENARIO_MODIFY,
    				'userId' => $userId,
    			]
    		],
    		'remove' => [
    			'class' => 'app\actions\FormAction',
    			'model' => [
    				'class' => 'app\forms\BookmarkForm',
    				'scenario' => BookmarkForm::SCENARIO_REMOVE,
    				'userId' => $userId,
    			]
    		],
    		'change-cate' => [
    			'class' => 'app\actions\FormAction',
    			'model' => [
    				'class' => 'app\forms\BookmarkForm',
    				'scenario' => BookmarkForm::SCENARIO_CHANGE_CATE,
    				'userId' => $userId,
    			]
    		],
    	];
    }
    
    public function actionList() {
        $post = \Yii::$app->getRequest()->post();
        
        $userId = \Yii::$app->user->getId();
        
        $query = Bookmark::find();
        $query->andWhere(['userId' => $userId]);
        
        if (isset($post['cateId'])) {
            $query->andWhere(['cateId' => intval($post['cateId'])]);
        }
        
        if (isset($post['keyword']) && !empty(trim($post['keyword']))) {
            $query->andWhere([
                'or', ['like', 'title', trim($post['keyword'])], ['like', 'url', trim($post['keyword'])]
            ]);
        }
        
        $query->orderBy('id desc');
        return ['data' => $query->all()];
    }
    
//     public function actionCreate() {
//     	$model = new BookmarkForm([
//     		'scenario' => BookmarkForm::SCENARIO_CREATE,
//     		'userId' => \Yii::$app->getUser()->getId()
//     	]);
    	
//     	$model->setAttributes(\Yii::$app->getRequest()->post());
//     	$this->runLogic(function() use ($model){
//     		$model->create();
//     	});
//     }
    
//     public function actionModify() {
//         return $this->work(BookmarkForm::SCENARIO_MODIFY);
//     }
    
//     public function actionRemove() {
//         return $this->work(BookmarkForm::SCENARIO_REMOVE);
//     }
    
//     public function work($scenario) {
//         $post = \Yii::$app->getRequest()->post();
//         $form = new BookmarkForm();
//         $form->setScenario($scenario);
//         $form->setAttributes($post);
//         $form->userId = \Yii::$app->user->getId();
//         if ($form->validate()) {
//             return ['data' => $form->work()];
//         } else {
//             return ['errors' => $form->getErrors()];
//         }
//     }
}

?>